<%# The license inside this block applies to this file.
	# Copyright 2017 Google Inc.
	# Licensed under the Apache License, Version 2.0 (the "License");
	# you may not use this file except in compliance with the License.
	# You may obtain a copy of the License at
	#
	#     http://www.apache.org/licenses/LICENSE-2.0
	#
	# Unless required by applicable law or agreed to in writing, software
	# distributed under the License is distributed on an "AS IS" BASIS,
	# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	# See the License for the specific language governing permissions and
	# limitations under the License.
-%>

if _, ok := d.GetOk("http_health_check"); ok {
	hc := d.Get("http_health_check").([]interface{})[0]
	ps := hc.(map[string]interface{})["port_specification"]
	pn := hc.(map[string]interface{})["port_name"]

	if ps == "USE_FIXED_PORT" || (ps == "" && pn == "") {
		m := obj["httpHealthCheck"].(map[string]interface{})
		if m["port"] == nil {
			m["port"] = 80
		}
	}
	obj["type"] = "HTTP"
	return obj, nil
}
if _, ok := d.GetOk("https_health_check"); ok {
	hc := d.Get("https_health_check").([]interface{})[0]
	ps := hc.(map[string]interface{})["port_specification"]
	pn := hc.(map[string]interface{})["port_name"]

	if ps == "USE_FIXED_PORT" || (ps == "" && pn == "") {
		m := obj["httpsHealthCheck"].(map[string]interface{})
		if m["port"] == nil {
			m["port"] = 443
		}
	}
	obj["type"] = "HTTPS"
	return obj, nil
}
if _, ok := d.GetOk("http2_health_check"); ok {
	hc := d.Get("http2_health_check").([]interface{})[0]
	ps := hc.(map[string]interface{})["port_specification"]
	pn := hc.(map[string]interface{})["port_name"]

	if ps == "USE_FIXED_PORT" || (ps == "" && pn == "") {
		m := obj["http2HealthCheck"].(map[string]interface{})
		if m["port"] == nil {
			m["port"] = 443
		}
	}
	obj["type"] = "HTTP2"
	return obj, nil
}
if _, ok := d.GetOk("tcp_health_check"); ok {
	hc := d.Get("tcp_health_check").([]interface{})[0]
	ps := hc.(map[string]interface{})["port_specification"]
	pn := hc.(map[string]interface{})["port_name"]

	if ps == "USE_FIXED_PORT" || (ps == "" && pn == "") {
		m := obj["tcpHealthCheck"].(map[string]interface{})
		if m["port"] == nil {
			m["port"] = 80
		}
	}
	obj["type"] = "TCP"
	return obj, nil
}
if _, ok := d.GetOk("ssl_health_check"); ok {
	hc := d.Get("ssl_health_check").([]interface{})[0]
	ps := hc.(map[string]interface{})["port_specification"]
	pn := hc.(map[string]interface{})["port_name"]

	if ps == "USE_FIXED_PORT" || (ps == "" && pn == "") {
		m := obj["sslHealthCheck"].(map[string]interface{})
		if m["port"] == nil {
			m["port"] = 443
		}
	}
	obj["type"] = "SSL"
	return obj, nil
}

if _, ok := d.GetOk("grpc_health_check"); ok {
	hc := d.Get("grpc_health_check").([]interface{})[0]
	ps := hc.(map[string]interface{})["port_specification"]
	pn := hc.(map[string]interface{})["port_name"]

	if ps == "USE_FIXED_PORT" || (ps == "" && pn == "") {
		m := obj["grpcHealthCheck"].(map[string]interface{})
		if m["port"] == nil {
			return nil, fmt.Errorf("error in HealthCheck %s: `port` must be set for GRPC health checks`.", d.Get("name").(string))
		}
	}
	obj["type"] = "GRPC"
	return obj, nil
}

return nil, fmt.Errorf("error in HealthCheck %s: No health check block specified.", d.Get("name").(string))
